Ext.define('Admin.view.gooutApprove.GooutApproveViewController', {
    extend: Ext.app.ViewController,
    alias: 'controller.gooutApproveViewController',
    onClickGooutApproveClaimButton: function (view, recIndex, cellIndex, item, e, record) {
        Ext.Ajax.request({
            url: 'goout/claim/' + record.get('taskId'),
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function (response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function () {
                        view.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
                }
            }
        });
    },
    onClickGooutApproveCompleteWindowButton: function (view, recIndex, cellIndex, item, e, record, btn) {
        var gridPanel = Ext.getCmp('gooutApproveGrid');
        if (gridPanel.getSelectionModel().hasSelection()) {
            var taskDefinitionKey = record.get('taskDefinitionKey');
            if (taskDefinitionKey == 'deptLeader') {
                this.setCurrentView('gooutDeptLeaderApproveForm', '部门领导审批');
            } else if (taskDefinitionKey == 'generalManager') {
                this.setCurrentView('gooutGeneralManagerApproveForm', '总经理审批');
            } else if (taskDefinitionKey == 'adminAssisant') {
                this.setCurrentView('adminAssisantForm', '填写车辆类型');
            } else if (taskDefinitionKey == 'hrManager') {
                this.setCurrentView('gooutHrManagerApproveForm', '人事经理审批');
            } else if (taskDefinitionKey == 'applyUser') {
                this.gooutGridOpenEditWindow(btn);
            }
        } else {
            Ext.Msg.alert('提示', '请选择一行数据');
        }

    },
    setCurrentView: function (view, title) {
        if (view) {
            var cfg = Ext.apply({
                xtype: 'gooutapprove-window',
                items: [Ext.apply({
                    xtype: view
                }, '')]
            }, {
                title: title
            });
            Ext.create(cfg);
        }
    },
    complete: function (url, variables, grid, form, id, pass, refuseReason) {
        var keys = '',
            values = '',
            types = '';
        if (variables) {
            Ext.each(variables, function (item) {
                if (keys != '') {
                    keys += ',';
                    values += ',';
                    types += ',';
                }
                keys += item.key;
                values += item.value;
                types += item.type;
            });
        }
        Ext.Ajax.request({
            url: url,
            method: 'post',
            params: {
                keys: keys,
                values: values,
                types: types,
                id: id,
                pass: pass,
                refuseReason: refuseReason
            },
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function (response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('操作成功', json.msg, function () {

                        form.up('window').close();
                        grid.getStore().reload();
                    });
                } else {
                    Ext.Msg.alert('操作失败', json.msg);
                }
            }
        });

    },
    onClickGeneralManagerApproveSubmitButton: function (btn) {
        var gridPanel = Ext.getCmp('gooutApproveGrid');
        var form = btn.up('form');
        var pass = form.getForm().findField('deptLeaderPass').getGroupValue();
        var refuseReason = '';
        var record = gridPanel.getSelectionModel().getLastSelected();
        var url = 'goout/complete/' + record.data.taskId;
        if (pass == true) {
            if (record.get('car') == '是') {
                var variables = [{
                    key: 'generalManagerApproved',
                    value: true,
                    type: 'B'
                        }, {
                    key: 'adminAssisant',
                    value: 'HR_STAFF',
                    type: 'S'
                    }];
            } else if (record.get('car') == '否') {
                var variables = [{
                    key: 'generalManagerApproved',
                    value: true,
                    type: 'B'
                        }];
            }
        } else {
            var variables = [{
                key: 'generalManagerApproved',
                value: false,
                type: 'B'
            }, {
                key: 'applyUser',
                value: record.data.name,
                type: 'S'
            }];
            refuseReason = form.getForm().findField('refuseReason').getValue();
        }


        this.complete(url, variables, gridPanel, form, record.data.id, pass, refuseReason);

    },
    onClickDeptLeaderApproveSubmitButton: function (btn) {
        var gridPanel = Ext.getCmp('gooutApproveGrid');
        var form = btn.up('form');
        var record = gridPanel.getSelectionModel().getLastSelected();
        var url = 'goout/complete/' + record.get('taskId');
        var pass = form.getForm().findField('deptLeaderPass').getGroupValue();
        var refuseReason = '';
        if (pass == true) {
            if (record.data.car == '是') {
                var variables = [{
                        key: 'deptLeaderApproved',
                        value: true,
                        type: 'B'
                 }, {
                        key: 'adminAssisant',
                        value: 'HR_STAFF',
                        type: 'S'
                    }
                 ];
            } else {
                var variables = [{
                    key: 'deptLeaderApproved',
                    value: true,
                    type: 'B'
                }];
            }
        } else {
            var variables = [{
                key: 'deptLeaderApproved',
                value: false,
                type: 'B'
            }, {
                key: 'applyUser',
                value: record.data.name,
                type: 'S'
            }];
            refuseReason = form.getForm().findField('refuseReason').getValue();
        }


        this.complete(url, variables, gridPanel, form, record.data.id, pass, refuseReason);
    },
    onClickHrManagerApproveSubmitButton: function (btn) {
        var gridPanel = Ext.getCmp('gooutApproveGrid');
        var form = btn.up('form');
        var record = gridPanel.getSelectionModel().getLastSelected();
        var url = 'goout/complete/' + record.get('taskId');
        var pass = form.getForm().findField('deptLeaderPass').getGroupValue();
        var refuseReason = '';

        if (pass == true) {

            var variables = [{
                key: 'hrApproved',
                value: true,
                type: 'B'
            }];

        } else {
            var variables = [{
                key: 'hrApproved',
                value: false,
                type: 'B'
            }, {
                key: 'applyUser',
                value: record.data.name,
                type: 'S'
            }];
            refuseReason = form.getForm().findField('refuseReason').getValue();
        }
        this.complete(url, variables, gridPanel, form, record.data.id, pass, refuseReason);
    },
    //    onClickDefendApproveSubmitButton: function () {
    //        var gridPanel = Ext.getCmp('gooutApproveGrid');
    //        var record = gridPanel.getSelectionModel().getLastSelected();
    //        var url = 'goout/complete/' + record.get('taskId');
    //        var variables = [];
    //
    //        Ext.Ajax.request({
    //            url: 'goout/Update_1',
    //            method: 'post',
    //            params: {
    //                id: record.data.id
    //            },
    //            async: false,
    //            defaultHeaders: {
    //                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
    //            },
    //            success: function (response, options) {
    //                Ext.Ajax.request({
    //                    url: url,
    //                    method: 'post',
    //                    params: {
    //                        var: variables,
    //                        id: record.data.id,
    //                        pass: true,
    //                        refuseReason: ''
    //                    },
    //                    defaultHeaders: {
    //                        "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
    //                    },
    //                    success: function (response, options) {
    //                        var json = Ext.util.JSON.decode(response.responseText);
    //                        if (json.success) {
    //                            Ext.Msg.alert('操作成功', json.msg, function () {
    //                                gridPanel.getStore().reload();
    //                            });
    //                        } else {
    //                            Ext.Msg.alert('操作失败', json.msg);
    //                        }
    //                    }
    //                });
    //            }
    //
    //        });
    //
    //
    //    },
    AdminAssisantSubmitForm: function (btn) {
        var form = btn.up('form').getForm();
        var form_1 = btn.up('form');
        var gridPanel = Ext.getCmp('gooutApproveGrid');
        var record = gridPanel.getSelectionModel().getLastSelected();
        var id = record.data.id;
        var remark = form.findField('remark').getValue();
        var carType = form.findField('carType').getGroupValue();
        var pass = true;
        Ext.Ajax.request({
            url: 'goout/Update',
            method: 'post',
            params: {
                id: id,
                remark: remark,
                carType: carType
            },
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            async: false
        });
        var url = 'goout/complete/' + record.get('taskId');
        var variables = [{
            key: 'hrManager',
            value: 'HR_MANAGER',
            type: 'S'
        }];
        var refuseReason = '';
        this.complete(url, variables, gridPanel, form_1, id, pass, refuseReason);
    },
    gooutGridOpenEditWindow: function (btn) {
        var grid = Ext.getCmp('gooutApproveGrid');
        var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];

            Ext.Ajax.request({
                url: 'goout/judge',
                method: 'post',
                params: {
                    ids: record.data.id
                },
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                success: function (response, options) {
                    var json = Ext.util.JSON.decode(response.responseText);
                    if (json.success) {
                        var gooutWindow = Ext.widget('gooutSAWindow', {
                            title: "修改外出申请表",
                            items: {
                                xtype: 'gooutForm'
                            }
                        });
                        gooutWindow.down('form').getForm().loadRecord(record);
                        grid.getStore().reload();
                    } else {
                        Ext.Msg.alert('操作失败', json.msg);
                    }
                }
            });
        } else {
            Ext.Msg.alert('提示', "请选择一行数据进行编辑");
        }

    },
    gooutApplyUpload: function (btn) {
        var grid = Ext.getCmp('gooutApproveGrid')
        var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];
            var url = 'goout/complete/' + record.get('taskId');

            var variables = [{
                key: 'reApply',
                value: true,
                type: 'B'
            }];

            var keys = '',
                values = '',
                types = '';
            if (variables) {
                Ext.each(variables, function (item) {
                    if (keys != '') {
                        keys += ',';
                        values += ',';
                        types += ',';
                    }
                    keys += item.key;
                    values += item.value;
                    types += item.type;
                });
            }
            Ext.Ajax.request({
                url: url,
                method: 'post',
                params: {
                    keys: keys,
                    values: values,
                    types: types,
                    id: record.data.id,
                    pass: true
                },
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                success: function (response, options) {
                    Ext.Ajax.request({
                        url: 'goout/start',
                        method: 'post',
                        params: {
                            id: record.data.id
                        },
                        defaultHeaders: {
                            "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                        },
                        success: function (response, options) {
                            var json = Ext.util.JSON.decode(response.responseText);
                            if (json.success) {
                                Ext.Msg.alert('操作成功', json.msg);

                                grid.getStore().reload();
                            } else {
                                Ext.Msg.alert('操作失败', json.msg);
                            }
                        }
                    });
                }
            });

        } else {
            Ext.Msg.alert('提示', "请选择一行数据进行申请");
        }

    },
    gooutApplyCancle: function (btn) {
        var grid = Ext.getCmp('gooutApproveGrid')
        var selModel = grid.getSelectionModel();
        if (selModel.hasSelection()) {
            var record = selModel.getSelection()[0];
            var url = 'goout/complete/' + record.get('taskId');

            var variables = [{
                key: 'reApply',
                value: false,
                type: 'B'
            }];

            var keys = '',
                values = '',
                types = '';
            if (variables) {
                Ext.each(variables, function (item) {
                    if (keys != '') {
                        keys += ',';
                        values += ',';
                        types += ',';
                    }
                    keys += item.key;
                    values += item.value;
                    types += item.type;
                });
            }
            Ext.Ajax.request({
                url: url,
                method: 'post',
                params: {
                    keys: keys,
                    values: values,
                    types: types,
                    id: record.data.id,
                    pass: true
                },
                defaultHeaders: {
                    "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
                },
                success: function (response, options) {
                    var json = Ext.util.JSON.decode(response.responseText);
                    if (json.success) {
                        Ext.Msg.alert('操作成功', json.msg);

                        grid.getStore().reload();
                    }
                }
            });
        } else {
            Ext.Msg.alert('提示', "请选择一行数据进行申请");
        }
    }
});
